package cn.com.haopy.yue.ai.admin.web;


import cn.com.haopy.yue.ai.admin.annotation.RequiresPermissionsDesc;
import cn.com.haopy.yue.ai.core.utils.ResponseUtil;
import cn.com.haopy.yue.ai.core.validator.Order;
import cn.com.haopy.yue.ai.core.validator.Sort;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskPriceStep;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskPriceStepService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @Description: TODO
 * @Author DuTao
 * @Date 2023/9/25 14:50
 */
@RestController
@RequestMapping("/admin/priceStep")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "价格区间")
public class AdminPriceStepController {
    private final Log logger = LogFactory.getLog(AdminPriceStepController.class);

    @Autowired
    private AIRiskPriceStepService priceStepService;

    @RequiresPermissions("admin:priceStep:list")
    @RequiresPermissionsDesc(menu = {"运营管理", "价格区间管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @Sort @RequestParam(defaultValue = "sort_index") String sort,
            @Order @RequestParam(defaultValue = "asc") String order) {
        Page<AIRiskPriceStep> adList = priceStepService.page(page, limit, sort, order);
        return ResponseUtil.okPage(adList);
    }

    @RequiresPermissions("admin:priceStep:create")
    @RequiresPermissionsDesc(menu = {"运营管理", "价格区间管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody AIRiskPriceStep ad) {
        priceStepService.save(ad);
        return ResponseUtil.ok(ad);
    }


    @RequiresPermissions("admin:priceStep:read")
    @RequiresPermissionsDesc(menu = {"运营管理", "价格区间管理"}, button = "详情")
    @GetMapping("/read")
    public Object read(@NotNull Integer id) {
        AIRiskPriceStep ad = priceStepService.getById(id);
        return ResponseUtil.ok(ad);
    }

    @RequiresPermissions("admin:priceStep:update")
    @RequiresPermissionsDesc(menu = {"运营管理", "价格区间管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody AIRiskPriceStep ad) {

        if (!priceStepService.updateById(ad)) {
            return ResponseUtil.updatedDataFailed();
        }
        return ResponseUtil.ok(ad);
    }

    @RequiresPermissions("admin:priceStep:delete")
    @RequiresPermissionsDesc(menu = {"运营管理", "价格区间管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody AIRiskPriceStep ad) {
        Integer id = ad.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        priceStepService.removeById(id);
        return ResponseUtil.ok();
    }
}
